package com.yb.xczx.security.resource.config;

import com.yb.core.model.WhiteListProperties;
import com.yb.xczx.security.resource.handler.XczxAccessDeniedHandler;
import com.yb.xczx.security.resource.handler.XczxAuthenticationEntryPoint;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.oauth2.server.resource.OAuth2ResourceServerConfigurer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.server.resource.introspection.OpaqueTokenIntrospector;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.util.CollectionUtils;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/5/23 14:10
 * FileName: AbstractOauth2TokenSecurityConfig
 * Description:
 */
@Slf4j
public abstract  class AbstractOauth2TokenSecurityConfig {
    @Bean
    PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }


    @Bean
    protected AccessDeniedHandler accessDeniedHandler(){
        return new XczxAccessDeniedHandler();
    }

    @Bean
    protected AuthenticationEntryPoint authenticationEntryPoint(){
        return new XczxAuthenticationEntryPoint();
    }

    @Autowired
    WhiteListProperties whiteListProperties;

    @Value("${spring.application.name}")
    String appName;

    @Bean
    @Order(2)
    public SecurityFilterChain resourceServerSecurityFilterChain(HttpSecurity http) throws Exception {
        // @formatter:off
        log.info("secure application {} whiteList:{}",appName,whiteListProperties.getUrls());
        http
                .authorizeHttpRequests((authorize) -> {
                    if (!CollectionUtils.isEmpty(whiteListProperties.getUrls())) {
                        authorize.antMatchers(whiteListProperties.getUrls().toArray(new String[whiteListProperties.getUrls().size()])).permitAll();
                    }
                    authorize.anyRequest().authenticated();
                });
        http.csrf().disable();

        this.formLogin(http);

        http.exceptionHandling()
                .authenticationEntryPoint(authenticationEntryPoint())
                .accessDeniedHandler(accessDeniedHandler());

        this.resourceServerConfig(http);
        // @formatter:on
        return http.build();
    }

    protected  void formLogin(HttpSecurity http){

    }

    protected  void resourceServerConfig(HttpSecurity http) throws Exception {

    }
    @Bean
    OpaqueTokenIntrospector opaqueTokenIntrospector(){
        return new CustomAuthoritiesOpaqueTokenIntrospector();
    }
}
